package com.gqr.lock.reentrantlock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * Copyright (C), 2020-2024,gqr
 * FileName:RecursionDemo
 * author:guqingrong
 * Date:2024-05-10 22:19
 * Description:递归获取可重入锁
 * History:
 * <author>    <time>    <version>    <desc>
 * 作者姓名     修改时间     版本号        描述
 */
public class RecursionDemo {

    private static ReentrantLock lock = new ReentrantLock();

    private static void accessResource(){
        lock.lock();
        try {
            System.out.println("已经对资源进行了处理");
            if(lock.getHoldCount()<5){
                System.out.println(lock.getHoldCount());
                accessResource();
                System.out.println(lock.getHoldCount());

            }

        }finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        accessResource();
    }
}
